package com.yichi.service;

import com.yichi.global.AppConfig;
import com.yichi.manager.MqttManager;

/**
 * mqtt 服务
 */
public class MqttService {
    private final static String TAG = "MqttService";

    private MqttManager mqttManager;
    private String deviceReceiveTopic = "";//接收主题
    private String deviceSendTopic = "";//发送主题

    /**
     * 单例模式
     */
    public static MqttService inst = new MqttService();

    private MqttService() {
    }

    public void init() {
        mqttManager = new MqttManager();
        mqttManager.setMessageCallback(new MqttHandle());
        mqttManager.connect();

        deviceReceiveTopic = String.format("cloud_to_bat_/%s", AppConfig.DEVICE_ID);
        deviceSendTopic = String.format("bat_to_cloud_/%s", AppConfig.DEVICE_ID);
        mqttManager.subscribeTopic(deviceReceiveTopic);
    }

    /**
     * 发送消息
     */
    public void sendMsg(String jsonString) {
        mqttManager.publishMessage(deviceSendTopic, jsonString);
    }
}
